//
//  ZHDropDownView.m
//  ZHDropDownView
//
//  Created by 正合适 on 2017/11/17.
//  Copyright © 2017年 Rainy. All rights reserved.
//

#import "ZHDropDownView.h"
#import "ZHDropSelectedView.h"

@interface ZHDropDownView ()

/**按钮ARR*/
@property (nonatomic, strong) NSMutableArray<UIButton *> *buttonsArr;
/**下拉框的宽度*/
@property (nonatomic, assign) float dropViewWidth;
/**配置*/
@property (nonatomic, strong) ZHDropDownConfig *config;
/**原点*/
@property (nonatomic, assign) CGPoint originalPoint;
/**选择view*/
@property (nonatomic, strong) ZHDropSelectedView *selectView;

@end

@implementation ZHDropDownView
#pragma mark - 生命周期
- (instancetype)initWithButtons:(NSArray<UIView *> *)buttons withConfig:(ZHDropDownConfig *)config originalPoint:(CGPoint)point {
    self = [super initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    if (self) {
        if (!buttons || buttons.count == 0) {
            // 没有按钮就不初始化界面
        } else {
            if (!config) {
                _config = [[ZHDropDownConfig alloc] init];
            } else {
                _config = config;
            }
            _originalPoint = point;
            _buttonsArr = buttons.mutableCopy;
            [self main];
        }
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    return [self initWithButtons:@[] withConfig:nil originalPoint:CGPointZero];
}

- (instancetype)init {
    return [self initWithButtons:@[] withConfig:nil originalPoint:CGPointZero];
}

#pragma mark - 入口
- (void)main {
    [self maxButtonWidth];
    [self dealConfig];
    [self createUI];
}

#pragma mark - 初始化界面
- (void)createUI {
    // 设置背景透明
    self.backgroundColor = _config.backgroundColor;
    // 点击移除手势view
    UIView *bgTapView = [[UIView alloc] initWithFrame:self.bounds];
    bgTapView.backgroundColor = kRGBA(0, 0, 0, 0);
    [self addSubview:bgTapView];
    _selectView = [[ZHDropSelectedView alloc] initWithButtons:_buttonsArr withConfig:_config];
    [self addSubview:_selectView];
    
    
    // 添加移除手势
    UITapGestureRecognizer *tapRemoveGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapRemoveAction)];
    [bgTapView addGestureRecognizer:tapRemoveGesture];
}

#pragma mark - 遍历数组，获取button的最大宽度
- (void)maxButtonWidth {
    _dropViewWidth = 0;
    for (UIButton *btn in _buttonsArr) {
        if (btn.frame.size.width>_dropViewWidth) _dropViewWidth = btn.frame.size.width;
    }
}

#pragma mark - 处理配置信息的默认值
- (void)dealConfig {
    if (_config.selectedViewWidth < _dropViewWidth) _config.selectedViewWidth = _dropViewWidth;
    if (_config.originalPoint.x != _originalPoint.x || _config.originalPoint.y != _originalPoint.y) _config.originalPoint = _originalPoint;    
}

#pragma mark - 点击移除Action
- (void)tapRemoveAction {
    [self remove];
}

#pragma mark - 移除
- (void)remove {
    [self removeFromSuperview];
}

#pragma mark - 显示
- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [_selectView showWithAnimation];
}

@end
